<?php
namespace app\index\controller;

use think\Controller;
use think\Exception;
use think\View;
use think\Db;
use Jssdk\JSSDK;
use think\Loader;
use Des3\DES3;
use WxpayAPI\WxPayNotify;
use Wechat\TPWechat;

class Activity extends Common {

    protected $wechatObj;
    /**
     * 将二维数组转换为字符串
     * */
    public function arr2str($arr){
        $sum = 0;
        $count = count($arr);
        for($i = 0; $i < $count; $i++){
            $sum .= $arr[$i]['id'].'-';
        }
        $sum = substr($sum,1);
        $sum = substr($sum, 0, -1);
        return $sum;
    }

    /**
	 * 上传图片
	 * */
    public function upload(){
        //要创建的多级目录
        $path=ROOT_PATH.'public'.'/uploads/image/'.date('Ymd');
        //判断目录存在否，存在给出提示，不存在则创建目录
        if (!is_dir($path)){
            //第三个参数是“true”表示能创建多级目录，iconv防止中文目录乱码
            $res=mkdir(iconv("UTF-8", "GBK", $path),0777,true);
            if (!$res){
                return json(['code' => -1, 'msg' => '系統繁忙，请稍后再试！']);
            }
        }
        $pic_path = $_FILES['fb_img']['tmp_name'];//图片的绝对路径
        $image = \think\Image::open($pic_path);
        $pic3 =  ROOT_PATH.'public'.'/uploads/image/'.date('Ymd').'/works_'.rand('100000','1000000000').'.jpg';
        $width = $image->width();
        $height = $image->height();
        $image->thumb($width,$height)->save($pic3);

        $remove_str = ROOT_PATH.'public';
        $db_path = str_replace($remove_str,'',$pic3);
        return json(['code' => 200, 'msg' => $db_path, 'path' => $pic3]);
    }

    /**
     * 获取活动报名数据s
     * @return \think\response\View
     */
    public function index() {
        // 获取文章列表
        $list = model('article')->where('category_id', 24)->order('create_time desc')->paginate(12);
        $this->assign('list', $list);
        return view();
    }


    /**
     * 文章详情
     */
    public function info() {

        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);
        }
        $id = input('param.id');

        // 修改文章点击数
        $hits = rand(1, 3);
        model('article')->where(['id' => $id])->setInc('hits', $hits);

        // 获取文章内容
        $data = model('article')->get(['id' => $id]);
        $this->assign('data', $data);

        // 获取分类内容
        $cate = model('category')->all(['pid' => $data->category->pid]);
        $this->assign('cate', $cate);

        // 获取上一篇
        $previous_posts =  model('article')->where(['id' => ['gt', $id]])->order('id asc')->find();
        $this->assign('previous_posts', $previous_posts);

        // 获取下一篇
        $next_chapter =  model('article')->where(['id' => ['lt', $id]])->order('id desc')->find();
        $this->assign('next_chapter', $next_chapter);

        // 获取推荐内容
        $recommend = model('article')->order('id desc')->limit(5)->select();
        $this->assign('recommend', $recommend);

        // 活动报名人数
        $num = db('huodongbaoming')->where('activity_id', $id)->count();
        $this->assign('num', $num);

        return view();
    }

    /**
     * 活动报名表单
     * @return \think\response\View
     */
    public function form() {
        $id = input('param.id');
        if (request()->isAjax()) {
            $mobile = input('param.mobile');
            $activity_id = input('param.activity_id');
            $hdbm = db('huodongbaoming')->where('mobile', $mobile)->where('activity_id',$activity_id)->find();
            if ($hdbm) {
                return json(['code' => 0, 'msg' => '该手机号码已申请报名']);
            }
            $data = [
                'activity_id' => input('param.activity_id'),
                'enterprise' => input('param.enterprise'),
                'linkman' => input('param.linkman'),
                'duty' => input('param.duty'),
                'mobile' => input('param.mobile'),
            ];
            $rs = db('huodongbaoming')->insert($data);
            if ($rs) {
                return json(['code' => 200, 'msg' => '报名成功']);
            } else {
                return json(['code' => 0, 'msg' => '报名失败']);
            }
        } else {
            // 获取文章内容

            $data = model('article')->get(['id' => $id]);
            $this->assign('data', $data);
            return view();
        }
    }

    /**
     * 酒文化节
     * @return \think\response\View
     */
    public function jwhj() {
        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);
        }

        $no_bay = db('jwh_order')->where('status', 2)->column('number');
        $this->assign('no_bay', $no_bay);
        return view();
    }

    /**
     * 生成订单
     */
    public function order() {
        try {
            $order_sn = 'JWH' .date('YmdHis') . rand(1000, 9999);
            $data = [
                'order_sn' => $order_sn,
                'enterprise' => input('param.enterprise'),
                'linkman' => input('param.linkman'),
                'position' => input('param.position'),
                'mobile' => input('param.mobile'),
                'number' => input('param.number'),
                'area' => input('param.area'),
                'create_time' => time()
            ];
            $rs = db('jwh_order')->insertGetId($data);
            if ($rs) {
                return json(['code' => 200, 'data' => ['order_id' => $rs]]);
            } else{
                return json(['code' => 0, 'msg' => '保存失败']);
            }
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }
    public function pay() {
        if (request()->isAjax()) {
            $order_id = input("param.order_id");
            $order = db('jwh_order')->where('id', $order_id)->find();
            if (!$order) {
                throw new \Exception("订单不存在");
            }

            $min_booth = ['L15', 'L19', 'L21', 'L16'];

            if (in_array($order['number'], $min_booth)) {
                // 订单金额
                $price = 0.01;
            } else {
                // 订单金额
                $price = 1000;
            }

            /**
             * 微信支付附加数据
             */
            $des3 = new DES3();
            $attach['sign'] = $des3->encrypt('hnjyxh.com');

            $view = new View();
            $wx_data = \app\api\service\Pay::wx_web_pay($order['order_sn'], $price, json_encode($attach));
            if ($wx_data['code'] != 200) {
                return json($wx_data);
            }
            $view->assign('order_id', $order_id);
            $view->assign('jsApiParameters', $wx_data['jsApiParameters']);
            $wxpay_html = $view->fetch('wx_pay');
            return json(array('code' => 200,'tpl' => $wxpay_html));

        } else {
            // 获取微信授权
            if (is_weixin()) {
                if (!session('?openid') && request()->action() != 'baseweixinlogin') {
                    session('base_url', url(request()->controller() . '/' . request()->action()));
                    session('base_param', input('param.'));
                    $this->oauth(); // 发起授权
                }
            }

                $order_id = input('param.order_id');
                $order = db('jwh_order')->where('id', $order_id)->find();
                if (!$order) {
                    $this->error('订单不存在');
                }
                $this->assign('order', $order);
                return view();
        }
    }

    public function enlist_pay() {
        if (request()->isAjax()) {
            $order_id = input("param.order_id");

            $order = db('enlist')->where('id',$order_id)->find();

            if (!$order) {
                throw new \Exception("订单不存在");
            }

            $price = 3999;

            /**
             * 微信支付附加数据
             */
            $des3 = new DES3();
            $attach['sign'] = $des3->encrypt('hnjyxh.com');
            $attach['enlist_type'] = 1;

            $view = new View();
            $wx_data = \app\api\service\Pay::wx_enlist_web_pay($order['order_sn'], $price, json_encode($attach));
            if ($wx_data['code'] != 200) {
                return json($wx_data);
            }
            $view->assign('order_id', $order_id);
            $view->assign('jsApiParameters', $wx_data['jsApiParameters']);
            $wxpay_html = $view->fetch('enlist_wx_pay');
            return json(array('code' => 200,'tpl' => $wxpay_html));

        } else {
            // 获取微信授权
            if (is_weixin()) {
                if (!session('?openid') && request()->action() != 'baseweixinlogin') {
                    session('base_url', url(request()->controller() . '/' . request()->action()));
                    session('base_param', input('param.'));
                    $this->oauth(); // 发起授权
                }
            }

            $order_id = input('param.order_id');
            $order = db('enlist')->where('id', $order_id)->find();
            if (!$order) {
                $this->error('订单不存在');
            }
            $this->assign('order', $order);
            return view();
        }
    }


    // 微信授权方法
    public function oauth()
    {
        $url = request()->domain() . url('index/activity/baseweixinlogin');
        $url = $this->getWechtObj()->getOauthRedirect($url, '', 'snsapi_base');
        exit('<script>window.location.href="' . $url . '"</script>');
    }

    protected function getWechtObj()
    {
        if ($this->wechatObj == null)
        {
            $options = config('wechat');
            $this->wechatObj = new TPWechat($options);
        }
        return $this->wechatObj;
    }

    // 微信回调页面
    public function baseweixinlogin()
    {
        $code = isset($_GET['code']) ? $_GET['code'] : '';
        if (!$code)
            $this->error('获取授权失败');

        $oauthAccessToken = $this->getWechtObj()->getOauthAccessToken();
        //dump($oauthAccessToken);

        if (!$oauthAccessToken)
            $this->error('获取授权失败');

        session('openid', $oauthAccessToken['openid']);
        /*if( session('activity') ==  'fbh') {
            // 授权成功, 记录微信信息
        } else {
            $userInfo = $this->getWechtObj()->getOauthUserinfo($oauthAccessToken['openid']);
            //dump($userInfo);exit;
            if (isset($userInfo['subscribe']) && $userInfo['subscribe'] == 0) {
                $param = session('base_param');
                if (isset($param['parent_openid'])) {
                    model('scan_log')->save(['p_openid' => $param['parent_openid'], 'openid' => $oauthAccessToken['openid']]);
                }
                $this->redirect(url('base/subscribe'));
            }
            if (!$userInfo) {
                unlink('./../runtime/access_token.json'); // 删除access_token 文件
                $this->oauth(); // 跳转重新发起授权请求
            }

            // 授权成功, 记录微信信息
            session('openid', $userInfo['openid']);
        }*/

        $this->redirect(session('base_url') . '?' . http_build_query(session('base_param')));
        return;
    }
   
    /**
     * 微信支付回调页面
     */
    public function notify()
    {
        $data = $GLOBALS['HTTP_RAW_POST_DATA'];
        $notify = new WxPayNotify();
        $notify->Handle(false);
    }

    /**
     * 支付成功
     * @return \think\response\View
     */
    public function pay_ok() {
//        $order_id = input('param.order_id');
//        $order = db('jwh_order')->where('id', $order_id)->find();
//        if (!$order) {
//            $this->error('订单不存在');
//        }
//        $this->assign('order', $order);
        return view();
    }
    /*
     * 博鳌酒业峰会报名成功
     * */
    public function enlist_pay_ok(){
        return view();
    }


    public function get_qr_code() {
        $order_id = input('param.order_id');
        // 二维码不存在 生成二维码
        Loader::import('QRcode.phpqrcode');

        // 处理路径
        $root_path = ROOT_PATH . 'public' . DS;
        $upload_path = 'uploads' . DS . 'qr_code' . DS;
        $file_name = $order_id . '.png';
        $dir = $root_path . $upload_path;
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }

        $erweima_image = $upload_path . $file_name; // 出库二维码地址
        $erweima_url = $root_path . $erweima_image;

        $erweima_text = input('param.url');

        \QRcode::png($erweima_text, $erweima_url, '', 6, 2); // 生成二维码

        $logo = $root_path . 'static/home/images/qr_logo.png';

        erweima_add_logo($erweima_url, $logo); // 添加logo

        return json(['code' => 200, 'path' => $erweima_image]);
    }

    /**
     * 理性饮酒周启动仪式邀请函
     * */
    public function drink(){
        if (request()->isAjax()) {
            $data = [
                'username' => input('param.username'),
                'sex' => input('param.sex'),
                'tel' => input('param.tel'),
                'company' => input('param.company'),
                'job' => input('param.job'),

            ];

            $is_tel = model('drink')->where(array('tel'=>$data['tel']))->count();
            if($is_tel == 0){
                $id = model('drink')->insertGetId($data);
                return json(['code' => 200,'msg' => '提交成功']);
            }else{
                return json(['code' => -1, 'msg' => '该手机号已被使用']);
            }
        }else{
            return view();
        }
    }

    /**
     * 征集酒字
     * @return \think\response\View
     */
    public function zjjz() {
        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);

        }
        return view();
    }

    /**
     * 征集酒字 作品上传页面
     * @return \think\response\Json|\think\response\View
     */
    public function zjjz_form() {
        if (request()->isAjax()) {
            $data = [
                'username' => input('param.username'),
                'mobile' => input('param.mobile'),
                'msg' => input('param.msg'),
                'image_1' => input('param.image_1', ''),
                'image_2' => input('param.image_2', ''),
                'image_3' => input('param.image_3', ''),
            ];
            $rs = model('zjjz')->save($data);
            if ($rs) {
                $this->success('提交成功！');
            } else {
                $this->error('系统繁忙！');
            }
            return json();
        } else {
            return view();
        }
    }
    /**
     * 珍湘酒广告语有奖征集
     * */
    public function zjjz_advert(){
        if (request()->isAjax()) {
            $data = [
                'username' => input('param.username'),
                'mobile' => input('param.mobile'),
                'advert' => input('param.advert'),
            ];

            $content = model('advert')->where('advert',$data['advert'])->count();

            if($content>0){
                return json(['code' => -1, 'msg' => '该广告语已经存在，请修改之后重新提交！']);
            }else{
                model('advert')->save($data);
                $draw_ed = model('advert')->where(array('mobile'=>$data['mobile'],'is_draw'=>1))->count();//已抽奖次数
                $draw_ing = model('advert')->where(array('mobile'=>$data['mobile']))->count();//广告提交次数
                if($draw_ing > 3){
                    $draw_ing = 3;
                }
                $count = $draw_ing-$draw_ed;//剩余抽奖次数

                $table = model('advert')->field('id,is_draw')->where(array('mobile'=>$data['mobile']))->limit(3)->buildSql();
                $info = Db::table($table.' t')->where(array('t.is_draw'=>0))->select();//查询未使用的有效的抽奖id
                $ids = $this->arr2str($info);
                return json(['code' => 1, 'count' => $count, 'ids' => $ids]);
            }
        } else {
            if(isset($_SERVER['HTTP_X_WAP_PROFILE'])){
                return view('zjjz_advert');
            }else{
                return view('pc_advert');
            }
        }
    }
    /**
     * 征集珍湘酒广告语结束页面
     * */
    public function zjjz_advert_end(){
        return view();
    }
    /**
     * 修改某条广告语的抽奖状态
     * */
    public function edit_draw(){
        $param = input('param.');
        $where = [
            'id' => $param['id']
        ];
        model('advert')->where($where)->update(['is_draw'=>1]);
    }

    /**
     * 悬赏两万元通缉珍湘酒
     * */
    public function works(){
        if (request()->isAjax()) {
            $data = [
                'username' => input('param.username'),
                'tel' => input('param.tel'),
                'img' => input('param.img'),
            ];
            model('works')->save($data);

            return json(['code' => 200, 'msg' => '作品上传成功！']);
        }else{
            return view();
        }
    }
    /**
     * 悬赏两万元通缉珍湘酒列表
     * */
    public function works_list(){
        $data = input('param.');
        if($data){
            $page = $data['page'];
            $list = model('works')->order('create_time desc')->limit($page*5,5)->select();
            foreach(json_decode(json_encode($list),'ture') as $k => $v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v['create_time']);
            }
            $end = count($list);
            return json(['code' => 200,'list' => $list, 'end' => $end]);
        }else{
            $list = model('works')->order('create_time desc')->limit(0,5)->select();

            foreach(json_decode(json_encode($list),'ture') as $k => $v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v['create_time']);
            }

            if(count($list) == 0){
                return view('no_data');
            }else{
                $this->assign('list', $list);
                return view();
            }
        }
    }

    /**
     * 已征集珍湘酒广告语列表
     * */
    public function zjjz_advertlist(){
        $data = input('param.');
        if($data){
            $page = $data['page'];
            $list = model('advert')->where(array('status'=>1,'delete'=>0))->order('create_time desc')->limit($page*10,10)->select();
            foreach(json_decode(json_encode($list),'ture') as $k => $v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v['create_time']);
                $list[$k]['advert'] = $v['advert'];
            }
            return json(['code' => 1,'list' => $list]);
        }else{
            $list = model('advert')->where(array('status'=>1,'delete'=>0))->order('create_time desc')->limit(10)->select();
            foreach(json_decode(json_encode($list),'ture') as $k => $v){
                $list[$k]['create_time'] = date("Y-m-d H:i:s",$v['create_time']);
                $list[$k]['advert'] = $v['advert'];
            }
            $this->assign('list', $list);
            return view();
        }
    }
    /**
     * 珍湘酒广告语投票评选
     * */
    public function wine_vote()
    {
        // 获取微信授权
        if (is_weixin()) {
            if (!session('?openid') && request()->action() != 'baseweixinlogin') {
                session('base_url', url(request()->controller() . '/' . request()->action()));
                session('base_param', input('param.'));
                $this->oauth(); // 发起授权
            }
        }

        $dateStr = date('Y-m-d', time());
        $timestamp0 = strtotime($dateStr);
        $timestamp24 = strtotime($dateStr) + 86400;

        $openid = session('openid');
        $advert_id = input('param.advert_id');
//        return json(['msg' => $advert_id]);
        if ($advert_id > 0) {
            $map['openid'] = $openid;
            $map['create_time'] = array(['>=', $timestamp0], ['<=', $timestamp24]);
            $info = model('vote')->where($map)->select();
            //不能投同一个广告语
            foreach ($info as $k => $v) {
                if ($v['advert_id'] == $advert_id) {
                    return json(['code' => -2, 'msg' => '一天内不能对同一个广告语多次投票！']);
                    exit;
                }
            }
            //每人每天只能投3票
            if (count($info) >= 3) {
                return json(['code' => -1, 'msg' => '您今日的投票次数已用完，请明日再来！']);
            } else {
                try {
                    $data = [
                        'openid' => $openid,
                        'advert_id' => input('param.advert_id'),
                        'create_time' => time()
                    ];
                    model('advert_vote')->where(['id' => input('param.advert_id')])->setInc('count', 1);
                    $rs = model('vote')->insertGetId($data);
                    if ($rs) {
                        return json(['code' => 200]);
                    } else {
                        return json(['code' => 0, 'msg' => '系统繁忙，请稍后再试！']);
                    }
                } catch (\Exception $e) {
                    return json(['code' => 0, 'msg' => $e->getMessage()]);
                }
            }
        } else {
            $map['openid'] = $openid;
            $map['create_time'] = array(['>=', $timestamp0], ['<=', $timestamp24]);

            $info = model('vote')->where($map)->select();//id
            $advert = model('advert_vote')->select();//数据
            $id = array();

            foreach($info as $k => $v){
                $id[] = $v['advert_id'];
            }

            $this->assign('info', $id ? $id : '');
            $this->assign('advert', $advert);
            return view();

            }

    }
    /**
     * 中国千商大会博鳌酒业峰会报名
     * */
    public function enlist(){

        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);
        }

        if (request()->isAjax()) {
            $order_sn = 'ENLIST' .date('YmdHis') . rand(1000, 9999);
            $data = [
                'username' => input('param.username'),
                'mobile' => input('param.mobile'),
                'email' => input('param.email'),
                'company' => input('param.company'),
                'address' => input('param.address'),
                'remark' => input('param.remark'),
                'sex' => input('param.sex'),
                'order_sn' => $order_sn,
            ];

            $is_mobile = model('enlist')->where(array('mobile'=>$data['mobile'],'status'=>1))->count();
            $is_email = model('enlist')->where(array('email'=>$data['email'],'status'=>1))->count();
            if($is_mobile == 0){
                if($is_email == 0){
                    $id = model('enlist')->insertGetId($data);
                    return json(['code' => 200,'msg' => $id]);
                }else{
                    return json(['code' => -2, 'msg' => '该邮箱已被使用']);
                }
            }else{
                return json(['code' => -1, 'msg' => '该手机号已被使用']);
            }
        }else{
            return view();
        }
    }
    /**
     * 兄弟连资料报填
    */
    public function addpeople(){
        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);

        }
        $id = input('param.id');
        $list = db("region")->select();
        $data = model('article')->get(['id' => $id]);
        $res = db('position')->order('id desc')->select();
        $this->assign('list', $list);
        $this->assign('data', $data);
        $this->assign('res',$res);
        return view();

    }

    /**
     * 兄弟连资料报填
     */
    public function zltb(){
        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);

        }
        $id = input('param.id');
        $list = db("region")->select(); // 所有大区
        $data = model('article')->get(['id' => $id]); // 活动信息
        $res = db('position')->select(); // 所有职务
        $provinces = model("regions")->all(['parent_id' => 0]); // 所有省份
        $this->assign('list', $list);
        $this->assign('data', $data);
        $this->assign('res',$res);
        $this->assign('provinces',$provinces);
        return view();
    }


    // 根据父级ID获取地址
    public function get_regions() {
        $pid = input('param.pid');
        $data = model("regions")->all(['parent_id' => $pid]); // 获取地址
        return json($data);
    }

    /**
     * 保存资料
     */
    public function zltb_save(){
        try {

            // 获取省市区
            if (input('?param.address')) {
                $address = model("regions")->where('region_id', 'in', input('param.address'))->column('region_name');
            } else {
                $address = model("regions")->where('region_id', 'in', [input('param.province'), input('param.city'), input('param.area')])->column('region_name');
            }
            $data = [
                'name' => input('param.name'),
                'sex' => input('param.sex'),
                'national' => input('param.national'),
                'birthday' => strtotime(input('param.birthday')),
                'height' => input('param.height'),
                'live' => input('param.live'),
                'native' => input('param.native'),
                'tel' => input('param.tel'),
                'email' => input('param.email'),
                'duty' => input('param.duty'),
                'head' => input('param.head'),
                'education' => input('param.education'),
                'majors' => input('param.majors'),
                'politik' => input('param.politik'),
                'school' => input('param.school'),
                'experience' => input('param.experience'),
                'hobby' => input('param.hobby'),
                'practice' => input('param.practice'),
                'evaluation' => input('param.evaluation'),
                'activity_id' => input('param.activity_id'),
                'QQ' => input('param.QQ'),
                'united' => input('param.united'),
                'contact' => input('param.contact', ''),
                'contactel' => input('param.contactel', ''),
                'dizi' => implode('',$address),
                'recommend' => input('param.recommend'),
                'recommendtel' => input('param.recommendtel'),
            ];
            $res = db("brothers")->where('tel', $data['tel'])->find();
            if ($res) {
                throw new Exception('请勿重复填报');
            }
            model("brothers")->save($data);
            return json(['code' => 200]);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }
    public function fbh(){
        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);

        }
        $id = input('param.id');
        $data = model('article')->get(['id' => $id]); // 活动信息
        $this->assign('data', $data);
        return view();

    }
    public function fbh2(){
        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);

        }
        $id = input('param.id');
        $data = model('article')->get(['id' => $id]); // 活动信息
        $this->assign('data', $data);
        return view();

    }
    public function fbh3(){
        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);

        }
        $id = input('param.id');
        $data = model('article')->get(['id' => $id]); // 活动信息
        $this->assign('data', $data);
        return view('fbher');

    }
    public function fbher(){
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $unit = $_POST['unit'];
        $duty = $_POST['duty'];
        $tel = $_POST['tel'];
        $list = model('fbher')->where(['tel'=>$tel])->find();
        if($list){
            echo '-1';exit;
        }
        $res = model("fbher")->save(['name'=>$name,'sex'=>$sex,'unit'=>$unit,'duty'=>$duty,'tel'=>$tel]);
        $lid = model("fbher")->where(['tel'=>$tel])->find();
        //$list = "insert into jyxh_fbh (`name`,`sex`,`unit`,`tel`) values('{$name}','{$sex}','{$unit}','{$tel}')";
        //$res =Db::execute($list);
        //echo model("fbh");getLastSql();s
        $id = $lid['id'];
        if($res){
            echo '1';exit;
        }else{
            echo '2';exit;
        }
    }
    public function insertfbh(){
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $unit = $_POST['unit'];
        $duty = $_POST['duty'];
        $tel = $_POST['tel'];
        $list = model('fbh')->where(['tel'=>$tel])->find();
        if($list){
            echo json_encode(array('code' => -1));exit;
        }
        $res = model("fbh")->save(['name'=>$name,'sex'=>$sex,'unit'=>$unit,'duty'=>$duty,'tel'=>$tel]);
        $lid = model("fbh")->where(['tel'=>$tel])->find();
        //$list = "insert into jyxh_fbh (`name`,`sex`,`unit`,`tel`) values('{$name}','{$sex}','{$unit}','{$tel}')";
        //$res =Db::execute($list);
        //echo model("fbh");getLastSql();s
        $id = $lid['id'];
        if($res){
            echo json_encode(array('code' => 1,'msg' =>$id));exit;
        }else{
            echo json_encode(array('code' => 2));exit;
        }
    }
    public function insertfbh1(){
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $unit = $_POST['unit'];
        $duty = $_POST['duty'];
        $tel = $_POST['tel'];
        $list = model('fbh1')->where(['tel'=>$tel])->find();
        if($list){
            echo '-1';exit;
        }
        $res = model("fbh1")->insert(['name'=>$name,'sex'=>$sex,'unit'=>$unit,'duty'=>$duty,'tel'=>$tel]);
        $lid = model("fbh1")->where(['tel'=>$tel])->find();
        //$list = "insert into jyxh_fbh (`name`,`sex`,`unit`,`tel`) values('{$name}','{$sex}','{$unit}','{$tel}')";
        //$res =Db::execute($list);
        //echo model("fbh");getLastSql();
        $id = $lid['id'];
        if($res){
            echo '1';exit;
        }else{
            echo '2';exit;
        }
    }

}
